package com.tianma315.admin.product.rest;


import java.util.Arrays;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.product.domain.DestructionDO;
import com.tianma315.admin.product.service.DestructionService;

/**
 * 
 * <pre>
 * 销毁信息
 * </pre>
 * <small> 2019-07-15 17:14:03 | wen</small>
 */
@Controller
@RequestMapping("/product/destruction")
public class DestructionController extends BaseController{
	@Autowired
	private DestructionService destructionService;
	
	@GetMapping()
	@RequiresPermissions("product:destruction:destruction")
	String Destruction(){
	    return "product/destruction/destruction";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("product:destruction:destruction")
	public Result<Page<DestructionDO>> list(Integer pageNumber, Integer pageSize, DestructionDO destructionDTO){
		// 查询列表数据
        Page<DestructionDO> page = destructionService.getDestructionPage(pageNumber,pageSize,destructionDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("product:destruction:add")
	String add(){
	    return "product/destruction/add";
	}

	@GetMapping("/edit/{destructionId}")
	@RequiresPermissions("product:destruction:edit")
	String edit(@PathVariable("destructionId") Integer destructionId,Model model){
		DestructionDO destruction = destructionService.selectById(destructionId);
		model.addAttribute("destruction", destruction);
	    return "product/destruction/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("product:destruction:add")
	public Result<String> save( DestructionDO destruction){
		destruction.setCreateBy(getUserId());
		if(destructionService.addDestruction(destruction)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("product:destruction:edit")
	public Result<String>  update( DestructionDO destruction){
		destructionService.updateById(destruction);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("product:destruction:remove")
	public Result<String>  remove( Integer destructionId){
		if(destructionService.deleteDestruction(destructionId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("product:destruction:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] destructionIds){
		for (Integer id:destructionIds) {
			destructionService.deleteDestruction(id);
		}
		return Result.ok();
	}
	
}
